//
//  XGUserDetailViewModel.swift
//  TodayNews
//
//  Created by monkey on 2020/2/12.
//  Copyright © 2020 itcast. All rights reserved.
//

import SwiftyJSON

class XGUserDetailViewModel
{
    /// 用户昵称
    var userName: String {
        return userDetail?.name ?? ""
    }
    
    /// 认证内容
    var verifiedContent: String? {
        return isUserVerified ? "头条认证: " + (userDetail?.verified_content ?? "") : nil
    }
    
    /// 地区
    var area: String? {
        let area = userDetail?.area
        return area != nil ? ("地区: " + area!) : nil
    }
    
    /// 简介
    var userDescription: String {
        return "简介: " + (userDetail?.description ?? "")
    }
    
    /// 用户id
    var userId: UInt64 {
        return userDetail?.user_id ?? 0
    }
    
    /// 是否认证
    var isUserVerified: Bool {
        return userDetail?.user_verified ?? false
    }
    
    /// 用户头像
    var avatar_url: String? {
        return userDetail?.avatar_url
    }
    
    /// 是否关注
    var isFollowing: Bool {
        return userDetail?.is_following ?? false
    }
    
    /// 顶部菜单
    var topTabs: [XGTopTab]? {
        return userDetail?.top_tab
    }
    /// 底部菜单
    var bottomTabs: [XGBottomTab]? {
        return userDetail?.bottom_tab
    }
    
    /// 关注关系是否改变
    var isFollowRelationChanged: Bool = false
    /// 粉丝数
    private(set) var followersCountStr: String = ""
    /// 关注数
    private(set) var followingsCountStr: String = ""
    /// 推荐用户数组
    private(set) var recommendUserCards:[XGUserCardModel]?
    
    /// 用户详情数据模型
    private var userDetail: XGUserDetailModel? {
        didSet {
            guard let userDetail = userDetail else {
                return
            }
            
            // 关注数
            followingsCountStr = userDetail.followings_count.countString + "关注"
            // 粉丝数
            followersCountStr = userDetail.followers_count.countString + "粉丝"
        }
    }
}

extension XGUserDetailViewModel
{
    /// 获取用户详情数据
    /// - Parameters:
    ///   - userId: 用户id
    ///   - completionHandle: 完成回调
    func fetchUserDetail(userId: UInt64,completionHandle: @escaping(Bool) -> Void) -> Void
    {
        AlamofireTool.loadUserDetailData(userId: userId) { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("加载用户详情数据失败!")
                completionHandle(false)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            let dict = jsonObject["data"].dictionaryObject             
            self.userDetail = XGUserDetailModel.deserialize(from: dict)
            completionHandle(true)
        }
    }
    
    /// 获取用户推荐数据
    /// - Parameters:
    ///   - userId: 用户id
    ///   - completionHandle: 完成回调
    func fetchUserRecommendRelation(userId: UInt64,completionHandle: @escaping(Bool) -> Void) -> Void
    {
        AlamofireTool.loadUserRecommendData(userId: userId) { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("加载推荐用户数据失败!")
                completionHandle(false)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            let userCards = jsonObject["user_cards"].array
            self.recommendUserCards = userCards?.compactMap {return XGUserCardModel.deserialize(from: $0.dictionaryObject)}
            completionHandle(true)
        }
    }
    
    /// 关注用户
    /// - Parameters:
    ///   - userId: 用户id
    ///   - completionHandle: 完成回调
    func followRelation(userId: UInt64,completionHandle: @escaping(Bool) -> Void) -> Void
    {
        AlamofireTool.loadFollowRelationData(userId: userId) { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("关注用户失败!")
                completionHandle(false)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            let value = jsonObject["message"].string
            completionHandle(value == "success")
        }
    }
    
    /// 取消关注用户
    /// - Parameters:
    ///   - userId: 用户id
    ///   - completionHandle: 完成回调
    func unfollowRelation(userId: UInt64,completionHandle: @escaping(Bool) -> Void) -> Void
    {
        AlamofireTool.loadUnfollowRelationData(userId: userId) { (responseObject, error) in
            if responseObject == nil || error != nil {
                XGPrint("取消关注用户失败!")
                completionHandle(false)
                return
            }
            
            let jsonObject = JSON(responseObject!)
            let value = jsonObject["message"].string
            completionHandle(value == "success")
        }
    }
}
